// pages/commentEdit/commentEdit.js
import * as API_Comment from '../../api/comment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentId: null,
    img:'',
    storeName:'',
    commit:true,
    comment:{},
    order:{},
    store:{},
    startList:[,,,,,],
    imgs:[],
    arrvideo:[],
    commentList: [],
    inputFocusStatus: false,
    sendData: {
      storeId: null,
      productId: null,
      pageNum: 1,
      pageSize: 10,
      pid: null
    },
    inputFocus: false,
    addCommentForm: {
      commentContent: '',
      orderId: null,
      pid: null
    }
  },
  handleManage (e) {
    let type = e.currentTarget.dataset.type
    let commentItem = this.data.comment
    let status = false
    let message = ''
    if (type === 'like') {
      status = commentItem.is_like
      message = '点赞成功'
    }
    if (type === 'collect') {
      status = commentItem.is_collect
      message = '收藏成功'
    }
    message = status ? '取消' + message : message
    let url = status ? '/comment/delCommentRelatedType' : '/comment/createCommentRelatedType'
    getApp().https.ajax({
      method: 'post',
      url: url,
      data: {
        commentId: this.data.sendData.pid,
        type: type
      },
      loading: true
    }).then(() => {
      wx.showToast({
        title: message,
        icon: 'none'
      })
      if (type === 'like') {
        this.setData({
          [`comment.is_like`]: !status,
          [`comment.likeNum`]: status ? commentItem.likeNum - 1 : commentItem.likeNum + 1
        })
      }
      if (type === 'collect') {
        this.setData({
          [`comment.is_collect`]: !status,
          [`comment.collectNum`]: status ? commentItem.collectNum - 1 : commentItem.collectNum + 1
        })
      }
    })
  },
  commentParse (e) {
    let id = e.currentTarget.dataset.parentId
    this.data.addCommentForm.pid = id
    this.setData({
      inputFocus: true
    })
  },
  sendComment () {
    getApp().https.ajax({
      method: 'post',
      url: '/comment',
      data: this.data.addCommentForm,
      loading: true,
      isJson: true
    }).then(() => {
      wx.showToast({
        title: '评论成功!',
        icon: 'none'
      })
      this.getList()
      this.setData({
        inputFocus: false,
        'addCommentForm.commentContent': ''
      })
    })
  },
  input (e) {
    this.setData({
      'addCommentForm.commentContent': e.detail.value
    })
  },
  inputBlur () {
    this.setData({
      inputFocusStatus: false
    })
  },
  inputFocus () {
    this.setData({
      inputFocusStatus: true
    })
  },
  clickInput () {
    this.data.addCommentForm.pid = this.data.sendData.pid
  },
  getDetail () {
    API_Comment.getInfo(this.data.sendData.pid).then(res=>{
      //debugger
      let arr=[];
      let arrvideo=[];
      if( res.data.commentImages){
        arr = res.data.commentImages.split(",")
      }
      if( res.data.commentVideo){
        arrvideo = res.data.commentVideo.split(",")
      }
      this.setData({
        comment:res.data,
        imgs:arr,
        videos:arrvideo,
        'addCommentForm.orderId': res.data.orderId
      })
    })
  },
  getList () {
    API_Comment.getList(this.data.sendData).then(res => {
      // let arr = this.data.commentList
      // for (let item of res.data.rows) {
      //   let arr = [];
      //   if (item.commentImages) {
      //     arr = item.commentImages.split(",");
      //   }
      //   let arrvideo = [];
      //   if (item.commentVideo) {
      //     arrvideo = item.commentVideo.split(",");
      //   }
      //   item.commentImages = arr
      //   item.commentVideo = arrvideo
      //   item.storeLevel = item.storeLevel.toFixed(1)
      // }
      // arr.push(...res.data.rows)
      let list = this.data.sendData.pageNum === 1 ? res.data.rows : [...this.data.commentList, ...res.data.rows]
      this.setData({
        commentList: list,
        finished: res.data.lastPage
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.sendData.pid = parseInt(options.commentId)
    this.data.sendData.storeId = parseInt(options.storeid)
    this.data.sendData.productId = parseInt(options.productId)
    // this.setData({
    //   commentId: parseInt(options.commentId)
    // })
    this.getDetail()
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})